

import time
import requests


#下载单张图片


class Aaa():
    headers = {
        "Cookie": "__cfduid=dfeaf16009458cb626353011331e826a71604114742",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
    }

    def get_cookies(self):
        url = "http://pic.netbian.com/"
        response = requests.get(url=url)
        self.headers ={
            "Cookie":"__cfduid=" + response.cookies["__cfduid"],
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
        }

     # 下载图片
    def download_image(self,image_src):
     try:

        title = str(time.time()).replace('.', '')
        image_path = "D:/image/" + title + ".png",
        image_path = list(image_path)
        response = requests.get(image_src,headers=self.headers)
        # 获取的文本实际上是图片的二进制文本
        img = response.content
        # 将他拷贝到本地文件 w 写  b 二进制  wb代表写入二进制文本
        with open(image_path[0],'wb' ) as f:
            print("下载图片："+title)
            f.write(img)
     except:
        self.get_cookies()




if __name__ == '__main__':
    aaa = Aaa()
    aaa.get_cookies()
    aaa.download_image("http://img.netbian.com/file/2019/1116/011e2850f150a5bd40e562a26c9872f8.jpg")
    # for i in range(2,100):
    #     url = "http://www.netbian.com/meinv/index_{}.htm".format(i)
    # aaa.download_image(url)
    time.sleep(10)